package hy;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.ArrayList;
import java.util.List;

public class DisCli {
    String url="192.168.188.100:2181,192.168.188.101:2181";
    int timeout=20000;
    ZooKeeper cli;
    void  getChilder()throws Exception{
        List<String> childs=cli.getChildren("/servers",true);
        System.out.println("========================="+childs);
        List<String>hosts=new ArrayList<>();
        for(String c:childs){
            byte[]data=cli.getData("/servers/"+c,false,null);
            hosts.add(new String(data));
        }
        System.out.println(hosts);
    }

    void getConnect()throws Exception{
        cli= new ZooKeeper(url, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    getChilder();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args)throws Exception {
        DisCli c=new DisCli();
        c.getConnect();
        Thread.sleep(Integer.MAX_VALUE);
    }
}